package com.chatdating.service.impl;

import com.chatdating.entity.User;
import com.chatdating.entity.dto.RegisterDto;
import com.chatdating.entity.vo.UserByNameVo;
import com.chatdating.mapper.UserMapper;
import com.chatdating.service.CosService;
import com.chatdating.service.UserService;
import com.chatdating.utils.EmptyUtil;
import com.chatdating.utils.SHA256Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public String getNameById(Integer authorId) {
        return userMapper.getNameById(authorId);
    }

    @Override
    public User findByNmme(String name) {
        return userMapper.findByName(name);
    }

    @Override
    public Integer save(User user) {
        User user1 = new User();
        BeanUtils.copyProperties(user,user1);
        // 1、密码加密
        String encryptedPassword = SHA256Util.encrypt(user.getPassword());
        user1.setPassword(encryptedPassword);
        // 5. 保存用户
        return userMapper.insert(user1);
    }

    @Override
    public Integer updateUserInfo(User user,MultipartFile head) {
        return null;
    }

    @Override
    public PageInfo<User> authenticatingUser(Integer pageNum, Integer pageSize, String username) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectUsersByUsername(username);
        return new PageInfo<>(list);
    }

    @Override
    public void passAuthentication(String uname) {
        userMapper.passAuthentication(uname);
    }

    @Override
    public void failAuthentication(String uname) {
        userMapper.failAuthentication(uname);
    }

    @Override
    public UserByNameVo getUserInfo(String username) {
        return userMapper.getUserInfo(username);
    }

    @Override
    public void updateUser(UserByNameVo user) {
        userMapper.updateUser(user);
    }

    @Override
    public Integer getAuthorIdByUsername(String username) {
       String authorId = userMapper.getAuthorIdByUsername(username);
       return Integer.parseInt(authorId);
    }

    @Override
    public void deleteUser(String username) {
        userMapper.deleteUser(username);
    }
}